﻿using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Lotto_WP7.Resources;
using Lotto.Core;

namespace Lotto_WP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        private ApplicationBarIconButton newNumbersButton;
        private ApplicationBarMenuItem aboutMenuItem;


        // Constructor
        public MainPage()
        {
            InitializeComponent();

            if (PhoneApplicationService.Current.State.ContainsKey("WinningNumbers"))
                // restore last shown numbers from tombstone
                this.lottoCtrl.SetNumbers((int[])PhoneApplicationService.Current.State["WinningNumbers"]);

            // create application bar

            this.newNumbersButton = new ApplicationBarIconButton(new Uri("/Icons/Refresh.png", UriKind.Relative));
            this.newNumbersButton.Text = Strings.NewNumbers;
            this.newNumbersButton.Click += new EventHandler(newNumbersButton_Click);

            this.ApplicationBar = new ApplicationBar();
            this.ApplicationBar.IsVisible = true;
            this.ApplicationBar.Opacity = 1;
            this.ApplicationBar.IsMenuEnabled = true;
            this.ApplicationBar.Buttons.Add(this.newNumbersButton);

            this.aboutMenuItem = new ApplicationBarMenuItem(Strings.AboutLotto);
            this.aboutMenuItem.Click += new EventHandler(aboutMenuItem_Click);
            this.ApplicationBar.MenuItems.Add(this.aboutMenuItem);
        }

        private void newNumbersButton_Click(object sender, EventArgs e)
        {
            // calculate numbers
            int[] lotteryNumbers = LotteryNumberCalculator.Calculate(6, 49);

            // show numbers
            this.lottoCtrl.SetNumbers(lotteryNumbers);

            // store numbers for later tombstoning
            PhoneApplicationService.Current.State["WinningNumbers"] = lotteryNumbers;
        }

        private void aboutMenuItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
        }
    }
}